# -*- coding: UTF-8 -*-

import pandas as pd
import numpy as np

# 设置输出窗口大小
desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 20)
np.set_printoptions(linewidth=desired_width)

start_timestamp = int(pd.to_datetime('2018-06-01 00:00:00', format="%Y-%m-%d %H:%M:%S").value / 1e9)
end_timestamp = int(pd.to_datetime('2018-11-19 00:00:00', format="%Y-%m-%d %H:%M:%S").value / 1e9)

threshold = 100
step_minute = 60 * 24

exchange_tx_path = '../data/'
save_path = 'largeTx.csv'
exchange_list = ['okex', 'bitmex', 'huobi', 'binance', 'bittrex', 'poloniex']


def get_large_tx(name):
    if start_timestamp > end_timestamp:
        return

    tx = pd.read_csv(exchange_tx_path + name + '.csv', header=-1, names=['time', 'value', 'tag', 'address'])
    tx['timestamp'] = pd.to_datetime(tx['time'], format="%Y%m%d%H%M%S").values.astype(np.int64) // 10 ** 9
    large_tx_info = []
    curr_time = start_timestamp
    while curr_time < end_timestamp:
        curr_tx = tx.loc[(tx['timestamp'] > (curr_time - step_minute * 60)) & (tx['timestamp'] <= curr_time) & (
                tx['value'] > threshold)]
        large_tx_info.append([pd.to_datetime(curr_time, unit='s'),
                              curr_tx.loc[curr_tx['tag'] == 0]['value'].sum(),
                              curr_tx.loc[curr_tx['tag'] == 1]['value'].sum(),
                              len(curr_tx)])
        curr_time += step_minute * 60

    large_tx_info = pd.DataFrame(large_tx_info, columns=['time', name + '_in_large_value',
                                                         name + '_out_large_value', name + '_large_tx_count'])
    return large_tx_info


if __name__ == '__main__':
    total_result = get_large_tx(exchange_list[0])
    for exchange_name in exchange_list[1:]:
        curr_df = get_large_tx(exchange_name)
        total_result = total_result.merge(curr_df, on='time', how='left')

    total_result.to_csv(save_path, index=False)
